<template>
  <div v-drag>
    <div class="mask"  @click="closePopup(false)"></div>
    <div class="edit_box" v-loading="loading">
      <div class="box_head">
        <p>{{did?'修改':'新增'}}系统配置</p>
        <i  @click="closePopup(false)"></i>
      </div>
      <div class="box_body" >
        <div class="line" :class="{'error_border':tip_num===0}">
          <span class="lab"><i class="mark_icon">*</i>键：</span>
          <div>
            <el-input type="text" placeholder="请输入键"  v-model.trim="dKey"
                      @input="setTip('','')"></el-input>
          </div>
        </div>
        <div class="line" :class="{'error_border':tip_num===1}">
          <span class="lab"><i class="mark_icon">*</i>值：</span>
          <div>
            <el-input type="text" placeholder="请输入值"  v-model.trim="dValue"
                      @input="setTip('','')"></el-input>
          </div>
        </div>
        <div class="line" :class="{'error_border':tip_num===2}">
          <span class="lab"><i class="mark_icon">*</i>类型：</span>
          <div>
            <el-input type="number" placeholder="请输入类型"  v-model.trim="dType"
                      @input="setTip('','')" :min="1"></el-input>
          </div>
        </div>
        <div class="line" :class="{'error_border':tip_num===3}">
          <span class="lab"><i class="mark_icon">*</i>类型标注：</span>
          <div>
            <el-input type="text" placeholder="请输入类型标注"  v-model.trim="typeName"
                      @input="setTip('','')"></el-input>
          </div>
        </div>
        <div class="line">
          <span class="lab">当前状态：</span>
          <div>
            <el-radio-group v-model="proStatus">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </div>
        </div>
        <p class="error_line">{{infoTip}}</p>
      </div>
      <div class="box_foot">
        <span @click="closePopup(false)">取消</span>
        <span @click="confirmEdit">确定</span>
      </div>
    </div>
  </div>
</template>

<script>
import { getDetail, addData, updateData } from "@/api/config/config";
export default {
  props:['did'],
  data(){
    return {
      dictId: '',
      dValue: '',
      dKey: '',
      dType: '',
      typeName:'',
      typeOptions:[],
      proStatus: 1,
      infoTip: '',
      tip_num: '',
      loading:false
    }
  },
  mounted() {
    if (this.did){
      this.getDetail();
    }
  },
  methods:{
    getDetail(){
      let that = this;
      that.loading = true;
      getDetail(that.did).then(res => {
        if (res){
          that.setData(res);
        }
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },

    setData(obj){
      this.dKey = obj.key?obj.key:'';
      this.dValue = obj.value?obj.value:'';
      this.proStatus = obj.enable?obj.enable:0;
      this.dType = obj.type?obj.type:'';
      this.typeName = obj.name ?obj.name:'';
    },

    // 确认编辑
    confirmEdit(){
      if (this.dKey === '') {
        this.setTip(0, '请输入键！')
      } else if (this.dValue === '') {
        this.setTip(1, '请输入值！');
      } else if (this.dType ===''||this.dType&&parseFloat(this.dType)<1) {
        this.setTip(2, '请输入类型！');
      } else if (this.typeName ==='') {
        this.setTip(3, '请输入类型名称！');
      } else {
        this.sendInfo();
      }
    },

    sendInfo(){
      let that = this;
      that.loading = true;
      let data = {
        type: that.dType,
        enable: that.proStatus,
        value: that.dValue,
        key: that.dKey,
        name:that.typeName
      }
      if (that.did) {
        data.id = that.did;
        updateData(JSON.stringify(data)).then(response => {
          that.closePopup(true);
          that.$message.success({
            message:'操作成功！',
            duration:1500
          });
          that.loading = false;
        }).catch((e) => {
          that.loading = false;
        });
      } else {
        addData(JSON.stringify(data)).then(response => {
          that.closePopup(true);
          that.$message.success({
            message:'操作成功！',
            duration:1500
          });
          that.loading = false;
        }).catch((e) => {
          that.loading = false;
        });
      }
    },

    setTip(num,text){
      this.tip_num = num;
      this.infoTip = text
    },

    // 关闭
    closePopup(refresh){
      this.$emit('listenToChild', {refresh: refresh})
    }
  }
}
</script>

<style scoped>


</style>
